package com.ticket.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.base.Splitter;
import com.ticket.common.utils.Const;
import com.ticket.common.utils.StringUtils;
import com.ticket.system.domain.TYanchuReservedSeats;
import com.ticket.system.mapper.TYanchuReservedSeatsMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.mapper.TYanchuReservedTicketMapper;
import com.ticket.system.domain.TYanchuReservedTicket;
import com.ticket.system.service.ITYanchuReservedTicketService;

/**
 * 预留座与座位关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-18
 */
@Service
public class TYanchuReservedTicketServiceImpl implements ITYanchuReservedTicketService 
{
    @Autowired
    private TYanchuReservedTicketMapper tYanchuReservedTicketMapper;
    @Autowired
    private TYanchuReservedSeatsMapper tYanchuReservedSeatsMapper;

    /**
     * 查询预留座与座位关联
     * 
     * @param id 预留座与座位关联主键
     * @return 预留座与座位关联
     */
    @Override
    public TYanchuReservedTicket selectTYanchuReservedTicketById(Long id)
    {
        return tYanchuReservedTicketMapper.selectTYanchuReservedTicketById(id);
    }

    /**
     * 查询预留座与座位关联列表
     * 
     * @param tYanchuReservedTicket 预留座与座位关联
     * @return 预留座与座位关联
     */
    @Override
    public List<TYanchuReservedTicket> selectTYanchuReservedTicketList(TYanchuReservedTicket tYanchuReservedTicket)
    {
        return tYanchuReservedTicketMapper.selectTYanchuReservedTicketList(tYanchuReservedTicket);
    }

    @Override
    public List<TYanchuReservedTicket> selectNewReservedTicketList(Long sessionId){
        return tYanchuReservedTicketMapper.selectNewReservedTicketList(sessionId);
    }

    /**
     * 新增预留座与座位关联
     * 
     * @param tYanchuReservedTicket 预留座与座位关联
     * @return 结果
     */
    @Override
    public int insertTYanchuReservedTicket(TYanchuReservedTicket tYanchuReservedTicket)
    {
        return tYanchuReservedTicketMapper.insertTYanchuReservedTicket(tYanchuReservedTicket);
    }

    /**
     * 修改预留座与座位关联
     * 
     * @param tYanchuReservedTicket 预留座与座位关联
     * @return 结果
     */
    @Override
    public int updateTYanchuReservedTicket(TYanchuReservedTicket tYanchuReservedTicket)
    {
        return tYanchuReservedTicketMapper.updateTYanchuReservedTicket(tYanchuReservedTicket);
    }

    /**
     * 批量删除预留座与座位关联
     * 
     * @param ids 需要删除的预留座与座位关联主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuReservedTicketByIds(Long[] ids)
    {
        return tYanchuReservedTicketMapper.deleteTYanchuReservedTicketByIds(ids);
    }

    /**
     * 删除预留座与座位关联信息
     * 
     * @param id 预留座与座位关联主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuReservedTicketById(Long id)
    {
        return tYanchuReservedTicketMapper.deleteTYanchuReservedTicketById(id);
    }

    /**
     * 获取某场次下预留座座位
     *
     * @param sessionId
     * @return
     */
    @Override
    public List<String> disposeResSeat(Long sessionId) {
        TYanchuReservedTicket ticket = new TYanchuReservedTicket();
        ticket.setSessionId(sessionId);
        List<TYanchuReservedTicket> tickets = tYanchuReservedTicketMapper.selectTYanchuReservedTicketList(ticket);
        List<String> keys = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(tickets)){
            tickets.forEach(t ->{
                String seats = t.getSeats();
                JSONArray seatArray = JSONArray.parseArray(seats);
                for (int i = 0; i < seatArray.size(); i++) {
                    JSONObject jsonObject = seatArray.getJSONObject(i);
                    Integer areaId = jsonObject.getInteger("areaId");
                    JSONArray seatJsonArray = jsonObject.getJSONArray("seats");
                    for (int j = 0; j < seatJsonArray.size(); j++) {
                        JSONObject seatJson = seatJsonArray.getJSONObject(j);
                        Long row = seatJson.getLong("row");//行
                        String seatNos = seatJson.getString("seatNo");//座位号
                        String cols = seatJson.getString("col");//坐标
                        List<String> seatNoList = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(seatNos);
                        List<String> colNoList = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(cols);
                        if(CollectionUtils.isEmpty(seatNoList) || CollectionUtils.isEmpty(colNoList)){
                            continue;
                        }
                        String key = areaId+"-"+row+"-";
                        for (int s = 0; s < seatNoList.size(); s++) {
                            keys.add(key.concat(seatNoList.get(s)).concat("-").concat(colNoList.get(s)));
                        }
                    }
                }
            });
        }
        return keys;
    }

    /**
     * 票房规划预留座信息
     *
     * @param tYanchuReservedTicket
     * @return
     */
    @Override
    public List<TYanchuReservedSeats> selectReservedTicketList(TYanchuReservedTicket tYanchuReservedTicket) {
        TYanchuReservedSeats seat = new TYanchuReservedSeats();
        seat.setOperaId(tYanchuReservedTicket.getOperaId());
        //seat.setStatue(1);
        List<TYanchuReservedSeats> seats = tYanchuReservedSeatsMapper.selectTYanchuReservedSeatsList(seat);
        if (CollectionUtils.isNotEmpty(seats) ){
            List<TYanchuReservedTicket> tickets = tYanchuReservedTicketMapper.selectTYanchuReservedTicketList(tYanchuReservedTicket);
            if (CollectionUtils.isNotEmpty(tickets)){
                seats.forEach(s ->{
                    tickets.forEach(t->{
                        if (s.getId().equals(t.getResSeatId())){
                            s.setSeats(t.getSeats());
                        }
                    });
                });
            }
            seats.removeIf(s -> Const.ZERO.equals(s.getStatue()) && StringUtils.isEmpty(s.getSeats()));
        }
        return seats;
    }

    /**
     * 删除场次下预留座信息
     *
     * @param sessionId
     */
    @Override
    public void deleteBySessionId(Long sessionId) {
        tYanchuReservedTicketMapper.deleteBySessionId(sessionId);
    }
}
